<?php
/**
 * Email: ye.fn@163.com
 *
 */

class pinyin
{
	var $_Str;
	
	function __construct()
    {
		$this->_Str = file_get_contents(PLUG_DIR . 'pinyin-utf8.dat');	
    }
	
	function turn($str)
	{
		if (YF_f::$config['charset'] != 'UTF-8')
			$str = mb_convert_encoding($str, 'UTF-8', YF_f::$config['charset']);
		
		$num = mb_strlen($str, 'UTF-8');
		$pyA = array();
		for($i=0; $i<$num; $i++)
		 {
		 	$b = mb_substr($str, $i, 1, 'UTF-8');

			if(	preg_match("/$b`([a-z]+)/",  $this->_Str, $arr) )
				$pyA[] = trim($arr[1]);
			else 
				$pyA[] = $b;//返回原字符
		 }
		
		return $pyA;
	}
    // 返回首字母，非字母数字用？代替
	function getFirstLetter($str,$n=0)
	{
		$py = '';
		foreach ($this->turn($str) as $v)
		 {
			if(preg_match("/^[a-zA-Z0-9]/",  $v))
				$py .= $v[0];
			else 
				$py .= '?';
		 }
		if($n)
			return substr($py,0,$n);
		return $py;
	}
}
?>